package cn.wolfcode.demo1.service.impl;

import cn.wolfcode.demo1.domain.User;
import cn.wolfcode.demo1.mapper.UserMapper;
import cn.wolfcode.demo1.service.UserService;
import cn.wolfcode.demo1.util.SmsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
/**
 * 将service对象交给容器管理
 */
public class UserServiceImpl implements UserService {
    /**
     * 调用mapper 层完成对数据的操作
     */
    @Autowired
    private UserMapper userMapper;

    @Override
    public void saveOrUpdate(User user) {
        /**
         * 新增 不需要传递 id  修改的过程中是需要用户 前台页面传递参数
         * 需要通过传递参数进行判断是进行新增还是修改
         */
        if (user.getId()!=null){
            userMapper.edit(user);
        }else {
            userMapper.add(user);
        }

    }

    @Override
    public void deleteById(Long id) {
        userMapper.delete(id);
    }

    @Override
    public User query(Long id) {
       return userMapper.selOneById(id);
    }

    @Override
    public List<User> selAll() {
       return userMapper.sllAll();
    }

    @Override
    public User login(String username, String password) {

        return userMapper.login(username,password);
    }

    @Override
    public void sendDX(String phone, int code) {
        SmsUtil.sendSmsAli(phone,String.valueOf(code));

    }
}
